<template/>
<script setup lang="ts" name="VariableBlock">
import {
  $insertNodes,
  COMMAND_PRIORITY_EDITOR,
  createCommand,
} from 'lexical'
import { mergeRegister } from '@lexical/utils'
import { useLexicalComposer } from 'lexical-vue'
import { CustomTextNode } from '../custom-text/node'
import { onMounted,onUnmounted } from 'vue'
import { INSERT_VARIABLE_BLOCK_COMMAND,INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from './common'


  const editor = useLexicalComposer()
  let unregisterListener: () => void
  onMounted(() => {
    unregisterListener = mergeRegister(
      editor.registerCommand(
        INSERT_VARIABLE_BLOCK_COMMAND,
        () => {
          const textNode = new CustomTextNode('{')
          $insertNodes([textNode])

          return true
        },
        COMMAND_PRIORITY_EDITOR,
      ),
      editor.registerCommand(
        INSERT_VARIABLE_VALUE_BLOCK_COMMAND,
        (value: string) => {
          const textNode = new CustomTextNode(value)
          $insertNodes([textNode])

          return true
        },
        COMMAND_PRIORITY_EDITOR,
      ),
    )
  })

  onUnmounted(()=> {
    unregisterListener?.()
  })


</script>